package cn.yy.cloudpay.view;

import cn.yy.cloudpay.R;
import android.app.Dialog;
import android.content.Context;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

public class LoadingDialog extends Dialog {
	
	private Context mContext;
	private String msg;
	private ImageView mIvLoading;
	
	public LoadingDialog(Context context,String msg) {
		this(context, R.style.Dialog,msg);
	}
	
	public LoadingDialog(Context context,int theme,String msg) {
		super(context, theme);
		this.mContext = context;
		this.msg = msg;
		initDialog();
		// 拦截多任务键
		getWindow().addFlags(5);
		// 拦截home键
		getWindow().addFlags(3);
	}
	
	private void initDialog() {
		LayoutInflater inflater = LayoutInflater.from(mContext); 
		View view = inflater.inflate(R.layout.dialog_loading, null);
		TextView tips = (TextView) view.findViewById(R.id.tv_dialog_states);
		mIvLoading = (ImageView) view.findViewById(R.id.iv_dialog_loading);
		setContentView(view,new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		tips.setText(msg);
		setCanceledOnTouchOutside(false);
		setCancelable(false);
	}

	@Override
	public void show() {
		super.show();
		Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.dialog_loading_animation);
		mIvLoading.setAnimation(animation);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_HOME
				|| keyCode == KeyEvent.KEYCODE_BACK) {
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}
}
